package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.vo.SplitBillDetailVo;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.SplitBillMapper;
import com.canaan.business.domain.inOutStock.SplitBill;
import com.canaan.business.service.inOutStock.ISplitBillService;
import com.canaan.common.annotation.TableName;
/**
 * 分捡单Service业务层处理
 *
 * @author canaan
 * @date 2025-03-29
 */
@Service
public class SplitBillServiceImpl implements ISplitBillService {
    @Resource
    private SplitBillMapper splitBillMapper;

    /**
     * 查询分捡单
     *
     * @param id 分捡单主键
     * @return 分捡单
     */
    @Override
    public SplitBill selectSplitBillById(Long id) {
        return splitBillMapper.selectSplitBillById(id);
    }

    /**
     * 查询分捡单列表
     *
     * @param splitBill 分捡单
     * @return 分捡单
     */
    @Override
    public List<SplitBill> selectSplitBillList(SplitBill splitBill) {
        return splitBillMapper.selectSplitBillList(splitBill);
    }

    /**
     * 新增分捡单
     *
     * @param splitBill 分捡单
     * @return 结果
     */
    @Override
    public int insertSplitBill(SplitBill splitBill) {
        if(StringUtils.isBlank(splitBill.getNo())){
            splitBill.setNo("S"+TableNoUtil.getNo(SplitBill.class));
        }
        splitBill.setCreateBy(SecurityUtils.getUsername());
        splitBill.setCreateTime(DateUtils.getNowDate());
        return splitBillMapper.insertSplitBill(splitBill);
    }

    /**
     * 批量新增分捡单
     *
     * @param splitBillList 分捡单列表
     * @return 结果
     */
    @Override
    public int batchInsertSplitBill(List<SplitBill> splitBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SplitBill splitBill : splitBillList){
            if(StringUtils.isBlank(splitBill.getNo())){
                splitBill.setNo("S"+TableNoUtil.getNo(SplitBill.class));
            }
            splitBill.setCreateBy(username);
            splitBill.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<splitBillList.size();) {
            int endIndex = i+500;
            if (endIndex > splitBillList.size()) {
                endIndex = splitBillList.size();
            }
            insertNum = insertNum + splitBillMapper.batchInsertSplitBill(splitBillList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改分捡单
     *
     * @param splitBill 分捡单
     * @return 结果
     */
    @Override
    public int updateSplitBill(SplitBill splitBill) {
        splitBill.setUpdateBy(SecurityUtils.getUsername());
        splitBill.setUpdateTime(DateUtils.getNowDate());
        return splitBillMapper.updateSplitBill(splitBill);
    }

    /**
     * 批量修改分捡单
     *
     * @param splitBillList 分捡单列表
     * @return 结果
     */
    @Override
    public int batchUpdateSplitBill(List<SplitBill> splitBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (SplitBill splitBill : splitBillList){
            splitBill.setUpdateBy(username);
            splitBill.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<splitBillList.size();) {
            int endIndex = i+500;
            if (endIndex > splitBillList.size()) {
                endIndex = splitBillList.size();
            }
            updateNum = updateNum + splitBillMapper.batchUpdateSplitBill(splitBillList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除分捡单
     *
     * @param ids 需要删除的分捡单主键集合
     * @return 结果
     */
    @Override
    public int deleteSplitBillByIds(Long[] ids) {
        return splitBillMapper.deleteSplitBillByIds(ids);
    }

    /**
     * 删除分捡单信息
     *
     * @param id 分捡单主键
     * @return 结果
     */
    @Override
    public int deleteSplitBillById(Long id) {
        return splitBillMapper.deleteSplitBillById(id);
    }

    /**
     * 查询分捡单
     *
     * @param no 编号
     * @return 分捡单
     */
    @Override
    public SplitBill selectSplitBillByNo(String no) {
        return splitBillMapper.selectSplitBillByNo(no);
    }

    /**
     * 批量删除分捡单
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteSplitBillByNos(String[] nos) {
        return splitBillMapper.deleteSplitBillByNos(nos);
    }

    /**
     * 删除分捡单信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteSplitBillByNo(String no) {
        return splitBillMapper.deleteSplitBillByNo(no);
    }

    /**
     * 根据托盘查询分拣数据
     * @param trayNo 托盘号
     * @return
     */
    @Override
    public List<SplitBillDetailVo> selectSplitBillDetailByTrayNo(String trayNo){
        return splitBillMapper.selectSplitBillDetailByTrayNo(trayNo);
    }
}
